package com.example.shop.fgo.ui.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.shop.fgo.R;
import com.example.shop.fgo.entity.FileListEntity;

import org.xutils.common.util.DensityUtil;

import java.util.List;

/**
 * Created by Administrator on 2017/2/13.
 */

public class PathNavi extends HorizontalScrollView implements View.OnClickListener {
    private Context mContext;
    private RelativeLayout mRelativeLayout;
    private LayoutInflater mLayoutInflater;
    private List<FileListEntity.PathEntity> mPathEntities;
    private OnNaviItemClickListener mOnNaviItemClickListener;

    public void setOnNaviItemClickListener(OnNaviItemClickListener onNaviItemClickListener) {
        mOnNaviItemClickListener = onNaviItemClickListener;
    }

    public void setPathEntities(List<FileListEntity.PathEntity> pathEntities) {
        //一旦对pathEntities进行设置了以后 进行各种设置
        if (mPathEntities==null) {
           //如果为空，则直接添加集合的零号元素
            addItem(pathEntities.get(0));
        }else{
            int oldCount=mPathEntities.size();
            int newCount=pathEntities.size();
            if (newCount>oldCount) {
                //最后一个是新打开的，则添加Item需加最新打开之前所有的
                addItem(pathEntities.get(pathEntities.size()-1));
            }else if (newCount<oldCount){
                    removeItem(oldCount-newCount);
            }
        }
        mPathEntities = pathEntities;

    }
    private void removeItem(int num){
        for (int i = 0; i < num; i++) {
            mRelativeLayout.removeViewAt(0);
        }
    }
    public PathNavi(Context context) {
        super(context);
    }

    public PathNavi(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        mRelativeLayout =new RelativeLayout(context);
        mRelativeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this.addView(mRelativeLayout);
        mLayoutInflater=LayoutInflater.from(mContext);
        this.setHorizontalScrollBarEnabled(false);
    }
    private void addItem(FileListEntity.PathEntity pathEntity){
        final TextView textView= (TextView) mLayoutInflater.inflate(R.layout.item_navi,mRelativeLayout,false);
        textView.setText(pathEntity.getName());
        textView.setTag(pathEntity);
        textView.setOnClickListener(this);
        View lastView = mRelativeLayout.getChildAt(0);
        if (lastView!=null) {
            RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) textView.getLayoutParams();
            layoutParams.leftMargin=lastView.getRight()- DensityUtil.dip2px(35);
            textView.setLayoutParams(layoutParams);
        }
        lastView=textView;
        //总是把最后加载的设置为第0个。
        mRelativeLayout.addView(textView,0);
        //避免textView.getRight()是0，不滚动  view绘制的三大流程  textView生成才有右定点
        textView.post(new Runnable() {
            @Override
            public void run() {
                //设置textview自动滑动
                PathNavi.this.smoothScrollBy(textView.getRight(),0);
            }
        });
    }

    @Override
    public void onClick(View v) {
        mOnNaviItemClickListener.onItemClick((FileListEntity.PathEntity) v.getTag());
    }

    public interface OnNaviItemClickListener{
            public void onItemClick(FileListEntity.PathEntity pathEntity);
    }
}
